package com.tuu.interceptor;


import com.tuu.utils.JWTUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
//@Slf4j  //implements HandlerInterceptor
public class MyJwtInterceptor  {

    //@Value("${jwt.token}")
    private String tokenHeader;

    //@Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        //首先从请求头中获取jwt串，与页面约定好存放jwt值的请求头属性名为User-Token
        System.out.println("进入interceptor");
        String jwt = httpServletRequest.getHeader(tokenHeader);
        //判断jwt是否有效
        if(StringUtils.isNotBlank(jwt)){
            System.out.println("有token");
            //System.out.println("interceptor_token为"+jwt);
           // JWTUtils.pureCheckJWT(jwt,httpServletRequest);
            if(JWTUtils.pureCheckJWT(jwt,httpServletRequest)){
                return true;
            }
        }else{
            System.out.println("没有token");
        }
        //输出响应流
        return false;
    }
    /*
    1）预处理；
    2）后处理（调用了Service并返回ModelAndView，但未进行页面渲染）；
    3）返回处理（已经渲染了页面）；

    在preHandle中，可以进行编码、安全控制等处理；
    在postHandle中，有机会修改ModelAndView；
    在afterCompletion中，可以根据ex是否为null判断是否发生了异常，进行日志记录。

     */
    //@Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

   // @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
